package org.jetnuke.common;

import org.apache.log4j.Logger;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TextUtilities {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(TextUtilities.class);

	private TextUtilities() {
	}

	final static Pattern ptAmp;

	final static Pattern ptLess;

	final static Pattern ptGreat;

	static {
		String regexp = "&(?!amp;)";
		ptAmp = Pattern.compile(regexp);
		regexp = "<";
		ptLess = Pattern.compile(regexp);
		regexp = ">";
		ptGreat = Pattern.compile(regexp);
	}

	public static String replaceAmp(String input) {
		if (logger.isDebugEnabled()) {
			logger.debug("replaceAmp(String) - start"); //$NON-NLS-1$
		}

		Matcher matcher = ptAmp.matcher(input);
		String returnString = matcher.replaceAll("&amp;");
		if (logger.isDebugEnabled()) {
			logger.debug("replaceAmp(String) - end"); //$NON-NLS-1$
		}
		return returnString;
	}

	public static String replaceTags(String input) {
		if (logger.isDebugEnabled()) {
			logger.debug("replaceTags(String) - start"); //$NON-NLS-1$
		}

		Matcher matcher = ptLess.matcher(input);
		input = matcher.replaceAll("&lt;");
		matcher = ptGreat.matcher(input);
		String returnString = matcher.replaceAll("&gt;");
		if (logger.isDebugEnabled()) {
			logger.debug("replaceTags(String) - end"); //$NON-NLS-1$
		}
		return returnString;
	}
}
